package com.lgc.common.base

import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import com.lgc.common.ktx.bindView
import com.lgc.common.ktx.viewLifeCycleOwner


abstract class BaseActivity<T:ViewDataBinding> : AppCompatActivity {
    //无参构造函数
    constructor():super()
    //有参构造函数
    constructor(@LayoutRes layout:Int):super(layout)

    protected lateinit var mBinding:T
    override fun onCreate(savedInstanceState: Bundle?) {
//        enableEdgeToEdge()//开启沉浸式 启用edge-to-edge全面屏体验
//        EdgeToEdge.enableEdgeToEdge(this)//开启全面屏
        super.onCreate(savedInstanceState)
        mBinding=bindView<T>(getLayoutRes()).also {
            it.lifecycleOwner=viewLifeCycleOwner
        }
        // 启用边缘到边缘模式
//        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
//            val window = window
//            window.insetsController?.let { controller ->
//                controller.enableEdgeToEdge() // 启用边缘到边缘模式
//            }
//        } else {
//            WindowCompat.setDecorFitsSystemWindows(window, false)
//        }
        initView()
        initConfig()
        initData()
    }

    @LayoutRes
    abstract fun getLayoutRes(): Int

    open fun initConfig(){}
    open fun initData(){}
    open fun initView(){}

    override fun onDestroy() {
        super.onDestroy()
        if(this::mBinding.isInitialized) {mBinding.unbind()}
    }
    /**
     * 扩展LiveData的observe函数
     */
    protected inline fun <T:Any> LiveData<T>.obseverKt(crossinline block:(T)->Unit){
        this.observe(viewLifeCycleOwner, Observer {
//            block.invoke(it)
            block(it)
        })
    }
}